package com.example.sell.service.impl;

import com.example.sell.dto.OrderDTO;
import com.example.sell.enums.ResultEnum;
import com.example.sell.exception.WechatOrderException;
import com.example.sell.service.OrderService;
import com.example.sell.service.PayService;
import com.example.sell.utils.JsonUtil;
import com.example.sell.utils.MathUtil;
import com.github.binarywang.wxpay.service.impl.PayScoreServiceImpl;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import com.lly835.bestpay.enums.BestPayTypeEnum;
import com.lly835.bestpay.model.PayRequest;
import com.lly835.bestpay.model.PayResponse;
import com.lly835.bestpay.model.RefundRequest;
import com.lly835.bestpay.model.RefundResponse;
import com.lly835.bestpay.service.impl.BestPayServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * @author: vince_zww
 * @date: 2020/8/24 15:04
 * @desceription:
 */
@Service
@Slf4j
public class PayServiceImpl implements PayService {

//    @Autowired
//    private WxPayServiceImpl wxPayService;

    private static final String ORDER_NAME = "微信点餐订单";

    @Resource
    private BestPayServiceImpl bestPayService;

    @Autowired
    private OrderService orderService;

    @Override
    public PayResponse create(OrderDTO orderDTO) {
        PayRequest payRequest = new PayRequest();
        payRequest.setOpenid(orderDTO.getOrderId());
        payRequest.setOrderAmount(orderDTO.getOrderAmount().doubleValue());
        payRequest.setOrderId(orderDTO.getOrderId());
        payRequest.setOrderName(ORDER_NAME);
        payRequest.setPayTypeEnum(BestPayTypeEnum.ALIPAY_H5);
        log.info("[微信支付] 发起支付 request={}", JsonUtil.toJson(payRequest));
        //测试时候会失败
//        PayResponse payResponse = bestPayService.pay(payRequest);
        PayResponse payResponse = new PayResponse();
        payResponse.setAppId("appId");
        payResponse.setOrderId("orderId");

        log.info("[微信支付] 发起支付 response] ={}", JsonUtil.toJson(payResponse));
        return payResponse;
    }

    @Override
    public PayResponse notify(String notifyData) {
        //1.验证签名
        //2.支付的状态
        //3.支付金额
        //4.支付人(下单人==支付人)

        PayResponse payResponse = bestPayService.asyncNotify(notifyData);
        log.info("[微信支付] 异步通知，payResponse={}",payResponse);
        //修改订单的支付状态
        OrderDTO orderDTO = orderService.findOne(payResponse.getOrderId());

        //判断订单是否存在
        if (orderDTO == null){
            log.info("[微信支付] 异步通知，订单不存在 orderId={}",payResponse.getOrderId());

            throw new WechatOrderException(ResultEnum.ORDER_NOT_EXIST);
        }
        //判断金额是否一致，注意精度和类型判断
        if (!MathUtil.equals(payResponse.getOrderAmount(),orderDTO.getOrderAmount().doubleValue())){
            log.info("[微信支付] 异步通知，订单金额不一致 orderId={}，微信通知金额={}. 系统金额={}",
                    payResponse.getOrderId(),
                    payResponse.getOrderAmount(),
                    orderDTO.getOrderAmount()
                    );
            throw new WechatOrderException(ResultEnum.WECHAT_APY_NOTIFY_MONEY_VERIFY_ERROR);
        }
        orderService.paid(orderDTO);
        return payResponse;
    }

    /**
     * 退款
     *
     * @param orderDTO
     */
    @Override
    public RefundResponse refund(OrderDTO orderDTO) {
        RefundRequest refundRequest = new RefundRequest();
        refundRequest.setOrderId(orderDTO.getOrderId());
        refundRequest.setOrderAmount(orderDTO.getOrderAmount().doubleValue());
        refundRequest.setPayTypeEnum(BestPayTypeEnum.WXPAY_MWEB);

        log.info("[微信退款] request={}",refundRequest);
        RefundResponse refundResponse = bestPayService.refund(refundRequest);
        log.info("[微信退款] response={}",refundResponse);

        return refundResponse;

    }
}
